import os
import sys
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget,
    QFileDialog,
    QMessageBox,
    QDesktopWidget,
)

from SentimentAnalysis import SentimentAnalysis
from TextCluster import TextClustering
from AssociationAnalyzer import AssociationAnalyzer
from TopicMining import TextClassification
from TopicMiningPro import TopicMiningPro
from TopicMiningMax import TopMiningMax


# 根据文件的绝对路径返回文件名
def get_filename_from_path(path):
    filename1 = os.path.basename(path)
    return os.path.splitext(filename1)[0]


class MainPage(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("数据挖掘分析")
        self.setGeometry(100, 100, 400, 300)
        self.center()

        layout = QVBoxLayout()

        self.emotion_button = QPushButton("情感分析")
        self.topic_button = QPushButton("主题提取")
        self.clustering_button = QPushButton("文本聚类")
        self.association_button = QPushButton("关联分析")
        self.exit_button = QPushButton("退出")

        layout.addWidget(self.emotion_button)
        layout.addWidget(self.topic_button)
        layout.addWidget(self.clustering_button)
        layout.addWidget(self.association_button)
        layout.addWidget(self.exit_button)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.emotion_button.clicked.connect(self.open_emotion_page)
        self.topic_button.clicked.connect(self.open_topic_page)
        self.clustering_button.clicked.connect(self.run_text_clustering)
        self.association_button.clicked.connect(self.run_association_analysis)
        self.exit_button.clicked.connect(self.close)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def open_emotion_page(self):
        self.emotion_page = EmotionPage(self)
        self.emotion_page.show()
        self.close()

    def open_topic_page(self):
        self.topic_page = TopicPage(self)
        self.topic_page.show()
        self.close()

    def run_text_clustering(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.txt);;All Files (*)"
        )
        if file_name:
            self.text_clustering(file_name)

    def run_association_analysis(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.txt);;All Files (*)"
        )
        if file_name:
            self.association_analysis(file_name)

    def text_clustering(self, file_name):
        filename = get_filename_from_path(file_name)
        julei = TextClustering(filename)
        julei.word_embedding()

        QMessageBox.information(self, "文本聚类", f"已对文件 {filename} 进行文本聚类！")

    def association_analysis(self, file_name):
        filename = get_filename_from_path(file_name)
        guanlian = AssociationAnalyzer(filename)
        guanlian.ConditionDevide()
        guanlian.StarDevide()
        guanlian.RelationAnalysis()
        QMessageBox.information(self, "关联分析", f"已对文件 {filename} 进行关联分析！")


class EmotionPage(QMainWindow):
    def __init__(self, parent):
        super().__init__()
        self.parent = parent
        self.setWindowTitle("情感分析")
        self.setGeometry(100, 100, 400, 300)
        self.center()

        layout = QVBoxLayout()

        self.corpus_button = QPushButton("语料库分析")
        self.naive_bayes_button = QPushButton("朴素贝叶斯分析")
        self.back_button = QPushButton("返回")

        layout.addWidget(self.corpus_button)
        layout.addWidget(self.naive_bayes_button)
        layout.addWidget(self.back_button)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.corpus_button.clicked.connect(self.run_corpus_analysis)
        self.naive_bayes_button.clicked.connect(self.run_naive_bayes_analysis)
        self.back_button.clicked.connect(self.go_back)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def run_corpus_analysis(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.txt);;All Files (*)"
        )
        if file_name:
            filename = get_filename_from_path(file_name)
            yuliao = SentimentAnalysis(filename)
            yuliao.sentiment_analysis1()
            QMessageBox.information(
                self, "语料库分析", f"已对文件 {filename} 进行语料库分析！"
            )

    def run_naive_bayes_analysis(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.txt);;All Files (*)"
        )
        if file_name:
            filename = get_filename_from_path(file_name)
            beiyesi = SentimentAnalysis(filename)
            beiyesi.sentiment_analysis2()

            QMessageBox.information(
                self, "朴素贝叶斯分析", f"已对文件 {filename} 进行朴素贝叶斯分析！"
            )

    def go_back(self):
        self.parent.show()
        self.close()


class TopicPage(QMainWindow):
    def __init__(self, parent):
        super().__init__()
        self.parent = parent
        self.setWindowTitle("主题提取")
        self.setGeometry(100, 100, 400, 300)
        self.center()

        layout = QVBoxLayout()

        self.keyword_button = QPushButton("关键词提取")
        self.logistic_regression_button = QPushButton("逻辑回归分类")
        self.api_button = QPushButton("星火大模型API调用")
        self.back_button = QPushButton("返回")

        layout.addWidget(self.keyword_button)
        layout.addWidget(self.logistic_regression_button)
        layout.addWidget(self.api_button)
        layout.addWidget(self.back_button)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.keyword_button.clicked.connect(self.run_keyword_extraction)
        self.logistic_regression_button.clicked.connect(self.run_logistic_regression)
        self.api_button.clicked.connect(self.run_api_call)
        self.back_button.clicked.connect(self.go_back)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def run_keyword_extraction(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.txt);;All Files (*)"
        )
        if file_name:
            filename = get_filename_from_path(file_name)
            guanjianci = TextClassification(filename)
            guanjianci.train_model()

            QMessageBox.information(
                self, "关键词提取", f"已对文件 {file_name} 进行关键词提取！"
            )

    def run_logistic_regression(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.txt);;All Files (*)"
        )
        if file_name:
            filename = get_filename_from_path(file_name)
            luojihuigui = TopicMiningPro(filename)
            luojihuigui.train_model()

            QMessageBox.information(
                self, "逻辑回归分类", f"已对文件 {file_name} 进行逻辑回归分类！"
            )

    def run_api_call(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.txt);;All Files (*)"
        )
        if file_name:
            filename = get_filename_from_path(file_name)
            damoxing = TopMiningMax(filename)
            damoxing.extract_features()

            QMessageBox.information(
                self,
                "星火大模型API调用",
                f"已对文件 {file_name} 进行星火大模型API调用！",
            )

    def go_back(self):
        self.parent.show()
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainPage()
    main_window.show()
    sys.exit(app.exec_())
